package model;

import java.net.*;
import java.sql.*;
import java.util.*;

import util.GetTime;

public class PhotoBoardDAO {
	
	public boolean insert(PhotoBoard pb){
		Connection conn = ConnectionManager.getConnection();
		try{
			PreparedStatement pstmt = conn.prepareStatement(
				"insert into photoboard (cono,pbname,mid,pbtitle,pbdate,pbcontent,pbcounter,"//7
				+ "pbsfn,pbofn,pbfsize,pbwidth,pbheight,pbno) values(?,?,?,?,?,?,0,?,?,?,?,?,pbseq.NEXTVAL)");
			pstmt.setInt(1, pb.getCono());
			System.out.print("pbcono = "+pb.getCono());
			
			pstmt.setString(2, pb.getPbname());
			pstmt.setString(3, pb.getMid());
			pstmt.setString(4, pb.getPbtitle());
			pstmt.setTimestamp(5, GetTime.nowTime);
			pstmt.setString(6, pb.getPbcontent());
			pstmt.setString(7, pb.getPbsfn());
			pstmt.setString(8, pb.getPbofn());
			pstmt.setInt(9, pb.getPbfsize());
			pstmt.setInt(10, pb.getPbwidth());
			pstmt.setInt(11, pb.getPbheight());
			pstmt.executeUpdate();
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
	}
	
	
	public boolean delete(int cono){
		String sql1 = "delete from photocomment where pbno in (select pbno from photoboard where cono=?)";
		String sql2 = "delete from photoboard where cono=?";
		
		Connection conn = ConnectionManager.getConnection();
		try{
			PreparedStatement pstmt1 = conn.prepareStatement(sql1);
			PreparedStatement pstmt2 = conn.prepareStatement(sql2);
			
			pstmt1.setInt(1, cono);
			
			pstmt2.setInt(1, cono);
			
			pstmt1.executeUpdate();
			pstmt2.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			return false;
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {			
				e.printStackTrace();
			}
		}
	}
	
	
	public boolean delete(int cono , String mid){
		Connection conn = ConnectionManager.getConnection();
		try{
			
			Statement stmt = conn.createStatement();
			stmt.executeUpdate("delete from photoboard where cono=" + cono + " and mid='"+mid+"'");
			
			return true;
		}catch(Exception e){
			
			e.printStackTrace();
			return false;
			
		}finally{
			
			if(conn!=null)try{conn.close();}catch(Exception e){}
			
		}
	}
	
	
	public boolean delete(PhotoBoard pb){
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			stmt.executeUpdate("delete from photoboard where pbno='" + pb.getPbno() + "'");
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
	}
	
	public boolean updateTextAndFile(PhotoBoard pb){
		Connection conn = ConnectionManager.getConnection();
		try{
			PreparedStatement pstmt = conn.prepareStatement(
				"update photoboard set pbtitle=?,pbcontent=?,pbsfn=?,pbofn=?," +
				"pbfsize=?,pbwidth=?,pbheight=? where pbno=?");
			pstmt.setString(1, pb.getPbtitle());
			pstmt.setString(2, pb.getPbcontent());
			pstmt.setString(3, pb.getPbsfn());
			pstmt.setString(4, pb.getPbofn());
			pstmt.setInt(5, pb.getPbfsize());
			pstmt.setInt(6, pb.getPbwidth());
			pstmt.setInt(7, pb.getPbheight());
			pstmt.setInt(8, pb.getPbno());
			pstmt.executeUpdate();
			return true;
		}catch(Exception e){
			return false;
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}	
	}
	
	public boolean updateText(PhotoBoard pb){
		Connection conn = ConnectionManager.getConnection();
		try{
			PreparedStatement pstmt = conn.prepareStatement(
				"update photoboard set pbtitle=?,pbcontent=? where pbno=?");
			pstmt.setString(1, pb.getPbtitle());
			pstmt.setString(2, pb.getPbcontent());
			pstmt.setInt(3, pb.getPbno());
			pstmt.executeUpdate();
			return true;
		}catch(Exception e){
			return false;
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
	}
	
	//get TotalRows of All PhotoBoard
	public long getTotalRows(){
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select count(*) from photoboard");
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
		return result;
	}
	
	
	/**
	 * 조성일
	 * 행당 커뮤니티 총레코드수 취득
	 * */
	public long getTotalRows(int cono){
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select count(*) from photoboard where cono=" + cono );
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
		return result;
	}
	
	/**
	 * 조성일
	 * 행당 커뮤니티 총레코드수 취득
	 * */
	public int getPbNo(int cono , String mid){
		int result = 0;
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select pbno from photoboard where cono=" + cono +"and mid='"+mid+"'");
			System.out.println("select pbno from photoboard where cono=" + cono +"and mid='"+mid+"'");
			rs.next();
			result = rs.getInt(1);
			rs.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
		return result;
	}
	
	//get TotalRows of PhotoBoard per BoardName in Community
	public long getBnameTotalRows(int cono, String pbname){
		
		
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(
				"select count(*) from photoboard where cono='" + cono + "' and pbname='" + pbname + "'");
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
		return result;
	}
	
	//get TotalRows of PhotoBoard per Community search Member
	public long getMemberTotalRows(int cono, String pbname, String mid){
		
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(
				"select count(*) from photoboard where cono='" + cono + "' and pbname='" + pbname + "' and mid='" + mid + "'");
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
		return result;
	}
	
	//get TotalRows of PhotoBoard per Community search Title
	public long getTitleTotalRows(int cono, String pbname, String title){
	
		long result = 0;
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(
				"select count(*) from photoboard where cono='" + cono + "' and pbname='" + pbname + "' and pbtitle like '%" + title + "%'");
			rs.next();
			result = rs.getLong(1);
			rs.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
		return result;
	}
	
	public Vector<PhotoBoard> getList(){
		Vector<PhotoBoard> photoboards = new Vector<PhotoBoard>();
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from photoboard");
			while(rs.next()){
				PhotoBoard photoboard = new PhotoBoard();
				photoboard.setPbno(rs.getInt(1));
				photoboard.setCono(rs.getInt(2));
				photoboard.setPbname(rs.getString(3));
				photoboard.setMid(rs.getString(4));
				photoboard.setPbtitle(rs.getString(5));
				photoboard.setPbdate(rs.getString(6));
				photoboard.setPbcontent(rs.getString(7));
				photoboard.setPbcounter(rs.getInt(8));
				photoboard.setPbsfn(rs.getString(9));
				photoboard.setPbofn(rs.getString(10));
				photoboard.setPbfsize(rs.getInt(11));
				photoboard.setPbwidth(rs.getInt(12));
				photoboard.setPbheight(rs.getInt(13));
				photoboards.add(photoboard);
			}
			rs.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
		return photoboards;
	}
	//해당하는 cono의 각 필드의 값을 가져오는 메소드..기존 메소드에 추가
	public Vector<PhotoBoard> getListPhotoCono(int cono,String pbname){
		
		int rownum = (int)getBnameTotalRows(cono,pbname)+1;//##
		Vector<PhotoBoard> photoboards = new Vector<PhotoBoard>();
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from photoboard where cono = "+cono+" and pbname='"+pbname+"' order by pbno desc");
			while(rs.next()){
				rownum--;
				PhotoBoard photoboard = new PhotoBoard();
				photoboard.setRownum(rownum);
				photoboard.setPbno(rs.getInt(1));
				photoboard.setCono(rs.getInt(2));
				photoboard.setPbname(rs.getString(3));
				photoboard.setMid(rs.getString(4));
				photoboard.setPbtitle(rs.getString(5));
				photoboard.setPbdate(rs.getString(6));
				photoboard.setPbcontent(rs.getString(7));
				photoboard.setPbcounter(rs.getInt(8));
				photoboard.setPbsfn(rs.getString(9));
				photoboard.setPbofn(rs.getString(10));
				photoboard.setPbfsize(rs.getInt(11));
				photoboard.setPbwidth(rs.getInt(12));
				photoboard.setPbheight(rs.getInt(13));
				photoboards.add(photoboard);
			}
			rs.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
		return photoboards;
	}
	
	
	public PhotoBoard getPhotoboard(int pbno){
		
		PhotoBoard photoboard = null;
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from photoboard where pbno='" + pbno + "'");
			rs.next();
			photoboard = new PhotoBoard();
			photoboard.setPbno(rs.getInt(1));
			photoboard.setCono(rs.getInt(2));
			photoboard.setPbname(rs.getString(3));
			photoboard.setMid(rs.getString(4));
			photoboard.setPbtitle(rs.getString(5));
			photoboard.setPbdate(rs.getString(6));
			photoboard.setPbcontent(rs.getString(7));
			photoboard.setPbcounter(rs.getInt(8));
			photoboard.setPbsfn(rs.getString(9));
			photoboard.setPbofn(rs.getString(10));
			photoboard.setPbfsize(rs.getInt(11));
			photoboard.setPbwidth(rs.getInt(12));
			photoboard.setPbheight(rs.getInt(13));
			rs.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
		return photoboard;
	}
	
	public void increaseCounter(int pbno){
		Connection conn = null;
		try {
			conn = ConnectionManager.getConnection();
			PreparedStatement pstmt = conn.prepareStatement(
				"update photoboard set pbcounter=pbcounter+1 where pbno=?");
			pstmt.setInt(1, pbno);
			pstmt.executeUpdate();
		} catch (Exception e){
			e.printStackTrace();
		} finally { 
			if(conn!=null)try {conn.close();}catch (Exception e) {}
		}
	}
	
	//get PhotoBoard List of Top i New Board
	public Vector<PhotoBoard> getTopNewList(int cono, String pbname, int i){
		int count = 0;//##
		int rownum = (int)getBnameTotalRows(cono,pbname)+1;//##
		Vector<PhotoBoard> newList = new Vector<PhotoBoard>();
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			/*@@ ResultSet rs = stmt.executeQuery(
				"select p.* from ( " +
					" select (@rownum:=@rownum+1)rownum, photo.* from ( " +
						" select pbno, pbtitle, DATE_FORMAT(pbdate,'%Y-%m-%d')pbdate, pbcounter, mid, pbsfn, (@rownum:=0)num " +
						" from photoboard where cono=" + cono + " and pbname='" + pbname + "'" + 
					" )photo " +
				")p order by pbno desc limit 0," + i
			);*/
			ResultSet rs = stmt.executeQuery("select * from photoboard where cono="+cono+"order by pbno desc");
			while(rs.next()){
				count++;//#*
				rownum--;
				if(count <= i){//*#
				PhotoBoard newboard = new PhotoBoard();
				newboard.setRownum(rownum);
				newboard.setPbno(rs.getInt("pbno"));
				newboard.setPbtitle(rs.getString("pbtitle"));
				newboard.setPbdate(rs.getString("pbdate"));
				newboard.setPbcounter(rs.getInt("pbcounter"));
				newboard.setMid(rs.getString("mid"));
				newboard.setPbname(pbname);
				newboard.setCono(cono);
				newboard.setPbsfn(rs.getString("pbsfn"));
				newList.add(newboard);
				}
			}
			rs.close();
			stmt.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try {conn.close();} catch(Exception e) {}
		}
		return newList;
	}
	
	//get PhotoBoard List of Top i Best Board
	public Vector<PhotoBoard> getTopBestList(int cono, String pbname, int i){
		int count = 0;//#*
		int rownum = (int)getBnameTotalRows(cono,pbname)+1;//##
		
		int pbcount[] = new int[3];//*#
		Vector<PhotoBoard> bestList = new Vector<PhotoBoard>();
		Vector<PhotoBoard> bestList1 = this.getListPhotoCono(cono,pbname);
		System.out.println("bestList1.size = "+bestList1.size());
		Connection conn = ConnectionManager.getConnection();
	
		try{
			Statement stmt = conn.createStatement();//##
			
			/*ResultSet rs = stmt.executeQuery(
				"select p.* from ( " +
					" select (@rownum:=@rownum+1)rownum, photo.* from ( " +
						" select pbno, pbtitle, DATE_FORMAT(pbdate,'%Y-%m-%d')pbdate, pbcounter, mid, pbsfn, (@rownum:=0)num " +
						" from photoboard where cono=" + cono + " and pbname='" + pbname + "'" + 
					" )photo " +
				")p order by pbcounter desc limit 0," + i
			);*/
			ResultSet rs = stmt.executeQuery("select * from photoboard where cono="+cono+" and pbname='"+pbname+"' order by pbcounter desc");//#*
			
			
			while(rs.next()){
				
				if(count < i){
					for(int j = 0; j<bestList1.size(); j++){
						
						if(bestList1.get(j).getPbcounter() == rs.getInt("pbcounter")){
							System.out.println("bestList1Ti = "+bestList1.get(j).getPbtitle());
							System.out.println("bestList1row = "+bestList1.get(j).getRownum());
							PhotoBoard bestboard = new PhotoBoard();
							bestboard.setRownum(bestList1.get(j).getRownum());
							bestboard.setPbno(rs.getInt("pbno"));
							bestboard.setPbtitle(rs.getString("pbtitle"));
							bestboard.setPbdate(rs.getString("pbdate"));
							bestboard.setPbcounter(rs.getInt("pbcounter"));
							bestboard.setMid(rs.getString("mid"));
							bestboard.setPbname(pbname);
							bestboard.setCono(cono);
							bestboard.setPbsfn(rs.getString("pbsfn"));
							bestList.add(bestboard);
							count++;
						}
							
				}
			}
			}
			rs.close();
			stmt.close();//*#
			
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try {conn.close();} catch(Exception e) {}
		}
		return bestList;
	}
	
	
	//get PhotoBoard List with Page per ViewType
	public Vector<PhotoBoard> getListPageQuery(long currentPage, long rowsPerPage, String viewType,int cono, String pbname){
		long startindex = (currentPage - 1) * rowsPerPage + 1;//1
	    long endindex = currentPage * rowsPerPage;//8
	    int rownum = (int)getBnameTotalRows(cono,pbname)+1;//##
	    //@@
	    int index = 0; 
		
		System.out.println("startIndex = "+startindex + "end ="+endindex+"rownum = "+rownum);
		
		
		
		Vector<PhotoBoard> result = new Vector<PhotoBoard>();
		Connection conn = ConnectionManager.getConnection();
		try {
			Statement stmt = conn.createStatement();
			/*@@ ResultSet rs = stmt.executeQuery(
				"select p.* from ( " +
					" select (@rownum:=@rownum+1)rownum, photo.* from ( " +
						" select pbno, pbtitle, DATE_FORMAT(pbdate,'%Y-%m-%d')pbdate, pbcounter, mid, pbsfn, cono, (@rownum:=0)num " +
						" from photoboard " + viewType + 
					" )photo " +
				")p order by pbno desc limit " + calculate + "," + rowsPerPage
			);*/
			
			ResultSet rs = stmt.executeQuery("select * from photoboard "+viewType+" order by pbno desc ");
			while(rs.next()) {
				 index += 1;  //@@ 모든 행에 인덱스번호 부여 1~끝까지
			     rownum -= 1;
			     if(index >= startindex && index <= endindex){
				//
				
				PhotoBoard photoboard = new PhotoBoard();
				//@@ photoboard.setRownum(rs.getInt("rownum"));
				photoboard.setRownum(rownum);
				photoboard.setPbno(rs.getInt("pbno"));
				photoboard.setPbtitle(rs.getString("pbtitle"));
				photoboard.setPbdate(rs.getString("pbdate"));
				photoboard.setPbcounter(rs.getInt("pbcounter"));
				photoboard.setMid(rs.getString("mid"));
				photoboard.setPbsfn(rs.getString("pbsfn"));
				cono = rs.getInt("cono");
				System.out.println("rownum = "+photoboard.getRownum()+"pbno = "+photoboard.getPbno()+"pbtitle = "+photoboard.getPbtitle());
				if(cono > 10){
					MemberCommunityDAO mcDAO = new MemberCommunityDAO();
					photoboard.setNickName(mcDAO.getCommunityNick(photoboard.getMid(), cono));
				}
				result.add(photoboard);
			
				
		
			}
			}
			rs.close();
			stmt.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try {conn.close();} catch(Exception e) {}
		}
		System.out.println("getListPageQuery의 사이즈 : "+result.size());
		return result;
	}
	
	//get PhotoBoard List with Page of All
	/*@@ public Vector<PhotoBoard> getPhotoPageAll(long currentPage, long rowsPerPage){
		String viewType = "";
		Vector<PhotoBoard> vector = getListPageQuery(currentPage, rowsPerPage, viewType);
		System.out.println("getPhotoPageAll의 사이즈 : "+vector.size());
		return vector;
	}*/
	
	//get PhotoBoard List with Page per BoardName in Community
	public Vector<PhotoBoard> getPhotoPageCommunity(long currentPage, long rowsPerPage, int cono, String pbname){
			
		String viewType = "where cono='" + cono + "' and pbname='" + pbname + "'";
		Vector<PhotoBoard> vector = getListPageQuery(currentPage, rowsPerPage, viewType, cono, pbname);
		
		System.out.println("getPhotoPageCommunity의 사이즈 : "+vector.size());
		return vector;
	} 
			
	//get PhotoBoard List with Page per Community search Member
	public Vector<PhotoBoard> getPhotoPageMember(long currentPage, long rowsPerPage, int cono, String pbname, String mid){
		String viewType = "where cono='" + cono + "' and pbname='" + pbname + "' and mid='" + mid + "'";
		Vector<PhotoBoard> vector = getListPageQuery(currentPage, rowsPerPage, viewType, cono, pbname);
		System.out.println("getPhotoPageMember의 사이즈 : "+vector.size());
		return vector;
	}
	
	//get PhotoBoard List with Page per Community search Title
	public Vector<PhotoBoard> getPhotoPageTitle(long currentPage, long rowsPerPage, int cono, String pbname, String title){
		String viewType = "where cono='" + cono + "' and pbname='" + pbname + "' and pbtitle like '%" + title + "%'";
		Vector<PhotoBoard> vector = getListPageQuery(currentPage, rowsPerPage, viewType, cono , pbname);
		System.out.println("getPhotoPageTitle의 사이즈 : "+vector.size());
		return vector;
	}
	
	//get Front PhotoBoard Number and Title
	public Vector getFrontNumTitle(int cono, String pbname, int pbno){
		System.out.println("7777");
		Vector frontNumTitle = new Vector();
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(
				//@@ "select pbno, pbtitle from photoboard where cono='" + cono + "'and pbname='" + pbname + "'and pbno>'" + pbno + "' order by pbno limit 1" 
					"select pbno, pbtitle from photoboard where cono='" + cono + "'and pbname='" + pbname + "'and pbno>'" + pbno + "' order by pbno"
			);
			if(rs.next()){
				frontNumTitle.add(rs.getInt("pbno"));
				frontNumTitle.add(rs.getString("pbtitle"));
			}else{
				frontNumTitle.add("");
				frontNumTitle.add("");
			}
			rs.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
		return frontNumTitle;
	}
	
	//get Next PhotoBoard Number and Title
	public Vector getNextNumTitle(int cono, String pbname, int pbno){
		Vector nextNumTitle = new Vector();
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(
				//@@ "select pbno, pbtitle from photoboard where cono='" + cono + "'and pbname='" + pbname + "'and pbno<'" + pbno + "' order by pbno desc limit 1" 
					"select pbno, pbtitle from photoboard where cono='" + cono + "'and pbname='" + pbname + "'and pbno<'" + pbno + "' order by pbno desc"
			);
			if(rs.next()){
				nextNumTitle.add(rs.getInt("pbno"));
				nextNumTitle.add(rs.getString("pbtitle"));
			}else{
				nextNumTitle.add("");
				nextNumTitle.add("");
			}
			rs.close();
			stmt.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null)try{conn.close();}catch(Exception e){}
		}
		return nextNumTitle;
	}
	
	public String[] getBoardMenus(int cono){
		Vector<String> v = new Vector<String>(); 
		String[] menus = null;
		String sql = "select distinct pbname from photoboard where cono=?";
		Connection conn = null;
		try{
			conn = ConnectionManager.getConnection();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, cono);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				v.add(rs.getString(1));
			}
			menus = new String[v.size()];
			for(int i=0;i<menus.length;i++){
				menus[i] = v.get(i);
			}
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			if(conn!=null)try {conn.close();}catch (Exception e) {}
		}
		return menus;
	}
	
	//커뮤니티 게시판 이름 수정 하는 메소드
	public void setBoardName(int cono, String pbname, String newname){
		
		String sql = "update photoboard set pbname=? where cono=? and pbname=?";
		Connection conn = ConnectionManager.getConnection();
		try{
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, newname);
			pstmt.setInt(2, cono);
			pstmt.setString(3, pbname);
			
			pstmt.executeUpdate();
		} catch(Exception e){
			e.printStackTrace();
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {			
				e.printStackTrace();
			}
		}
	
	}
	
	//커뮤니티 게시판 삭제하는 메소드
	public void deleteBoard(int cono, String pbname){
		String sql1 = "delete from photocomment where pbno in (select pbno from photoboard where cono=? and pbname=?)";
		String sql2 = "delete from photoboard where cono=? and pbname=?";
	
		
		Connection conn = ConnectionManager.getConnection();
		try{
			PreparedStatement pstmt1 = conn.prepareStatement(sql1);
			PreparedStatement pstmt2 = conn.prepareStatement(sql2);
			
			pstmt1.setInt(1, cono);
			pstmt1.setString(2, pbname);
			
			pstmt2.setInt(1, cono);
			pstmt2.setString(2, pbname);
			
			pstmt1.executeUpdate();
			pstmt2.executeUpdate();
			
		} catch(Exception e){
			e.printStackTrace();
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {			
				e.printStackTrace();
			}
		}
	}
	
	//포토게시판 별로 포토파일의 이름들 가져오기//////////////////////////
	public Vector<String> getPhotoFileNames(int cono, String pbname){
		String sql = "select pbsfn from photoboard where cono=? and pbname=?";
		Connection conn = ConnectionManager.getConnection();
		Vector<String> pfn = new Vector<String>();
		try{
			PreparedStatement pstmt = conn.prepareStatement(sql);
			
			pstmt.setInt(1, cono);
			pstmt.setString(2, pbname);
			
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				pfn.add(rs.getString("pbsfn"));
			}
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			if(conn!=null)try {conn.close();}catch (Exception e) {}
		}
		return pfn;
	}
	
	//커뮤니티 게시판 삭제시 저장된 파일명 전부 가져오기 
	public Vector<String> getPhotoFileNames(int cono){
		String sql = "select pbsfn from photoboard where cono=?";
		Connection conn = ConnectionManager.getConnection();
		Vector<String> pfn = new Vector<String>();
		try{
			PreparedStatement pstmt = conn.prepareStatement(sql);
			
			pstmt.setInt(1, cono);
			
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				pfn.add(rs.getString("pbsfn"));
			}
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			if(conn!=null)try {conn.close();}catch (Exception e) {}
		}
		return pfn;
	}
	
}
